@using CleanArchitecture.Blazor.Application.Features.Visitors.Commands.Approve
@using CleanArchitecture.Blazor.Application.Features.Visitors.DTOs
@using CleanArchitecture.Blazor.Application.Features.Visitors.Queries.Pagination
@using CleanArchitecture.Blazor.Application.Features.Visitors.Queries.Search
@using System.Globalization
@inject IStringLocalizer<Visitors> L
@inherits MudComponentBase
<div class="d-flex flex-column">
    <MudPaper Class="pa-4 mt-4">
        <MudTextField @bind-Value="_keyword" Label="@L["Search for visitor's email or phone number."]" Variant="Variant.Outlined" Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.Search" AdornmentColor="MudBlazor.Color.Primary" OnAdornmentClick="SearchPendingVisitor" />
    </MudPaper>
    <MudPaper Class="mt-4">
        <MudList Clickable="true">
            <MudListSubheader>
                @L["Pending Confirm"] (@_list.Count)
            </MudListSubheader>
            @foreach (var item in _list)
            {
                <MudListItem>
                    <div class="d-flex flex-row flex-grow-1 gap-4">
                        <div class="d-flex flex-column flex-grow-1">
                            <MudText Typo="Typo.body1">@item.Name</MudText>
                            <MudText Typo="Typo.body2">@item.CompanyName</MudText>
                            <MudText Typo="Typo.body2">@L["Check-in Date"]:@item.CheckinDate</MudText>
                            <MudText Typo="Typo.body1">@L["Companion Count"]: @item.CompanionCount</MudText>
                        </div>
                        <div class="d-flex flex-column flex-grow-0">
                            <MudButton Class="ml-auto"
                                       Disabled="@(item._processing)"
                                       EndIcon="@Icons.Filled.FactCheck"
                                       Variant="Variant.Text"
                                       OnClick="@(()=>Confirm(item))"
                                       Color="MudBlazor.Color.Primary">
                                @if (item._processing)
                                {
                                    <MudProgressCircular Class="ms-n1" Size="MudBlazor.Size.Small" Indeterminate="true" />
                                    <MudText Class="ms-2">@L["Processing"]</MudText>
                                }
                                else
                                {
                                    <MudText>@L["Confirm"]</MudText>
                                }
                            </MudButton>
                           
                        </div>
                    </div>
                </MudListItem>
            }

        </MudList>
    </MudPaper>
</div>

@code {

    private string? _keyword { get; set; }
    [Inject]
    private ISender _mediator { get; set; } = default!;
    private List<VisitorDto> _list { get; set; } = new();
    protected override async Task OnInitializedAsync()
    {
        await SearchPendingVisitor();
    }
    public async Task SearchPendingVisitor()
    {
        _list = await _mediator.Send(new SearchPendingConfirmVisitorsQuery(_keyword));
        StateHasChanged();
    }

    public async Task Confirm(VisitorDto item)
    {
        item._processing = true;
        try
        {
            var result = await _mediator.Send(new ConfirmVisitorCommand(new int[] { item.Id }));
            Snackbar.Add($"{string.Format(L["{0} confirmed"], item.Name)}", MudBlazor.Severity.Success);
            await SearchPendingVisitor();
        }
        finally
        {
            item._processing = false;
        }

    }



}
